package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.oilPointPojo;
import com.example.service.oilPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
 * ClassName:oilPointController
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/6/23 20:37
 * @Version 1.0
 */
@RestController
@Slf4j
public class oilPointController {
    @Autowired
    private oilPointService oilPointService;
    //新增加一条完整的记录。
    @PostMapping("/oilPointInsertOne")
    public Result insertOne(@RequestBody oilPointPojo oilPoint){
        oilPointService.insertOne(oilPoint);
        return Result.success();
    }
    //查询所有的列表信息
    @PostMapping("/oilPointSelectAllList")
    public Result selectAllList(){
        List<oilPointPojo> oilPointPojoList=oilPointService.selectAllList();
        return Result.success(oilPointPojoList);
    }
    //修改某一行的数据
    @PostMapping("/oilPointUpListOne")
    public Result oilPointUpListOne(@RequestBody oilPointPojo oilPoint){
        oilPointService.ilPointUpListOne(oilPoint);
        return Result.success();
    }
    //上传加油点位置照片的控制层
    @PostMapping("/oilPointAnnex")
    public Result oilPointAnnex(@RequestParam("file") MultipartFile file,@RequestParam Integer id,@RequestParam String oilPointAnnex) {

        String allName = oilPointService.oilPointAnnex(file,id,oilPointAnnex);
        return Result.success(allName);
    }
    //上传加油点作业指导书照片的控制层
    @PostMapping("/oilBookAnnex")
    public Result oilBookAnnex(@RequestParam("file") MultipartFile file,@RequestParam Integer id,@RequestParam String oilBookAnnex) {

        String allName = oilPointService.oilBooKAnnex(file,id,oilBookAnnex);
        return Result.success(allName);
    }
        //删除文件的函数
    @PostMapping("/oilPictureRemove")
    public Result oilPictureRemove(@RequestBody Map<String, Object> parameters) {
        String fileName=oilPointService.oilPictureRemove(parameters);
        return Result.success(fileName);
    }
    //读取文件，返回给前端，用作保存数据用
    @GetMapping("/oilDownload")
    public Result oilDownload(@RequestParam("fileName") String fileName){
        byte[] file=oilPointService.readFile(fileName);
        return Result.success(file);
    }
    //修改一个字段，传来的参数是需要修改的ID，字段名称，字段变量，修改的是字符串变量
    @PostMapping("/editOilOneFieldText")
    public Result editOilOneFieldText(@RequestBody Map<String, Object> parameters) {
        String fileName=oilPointService.editOilOneFieldText(parameters);
        return Result.success(fileName);
    }
}
